﻿// -----------------------------------------------------------------------
// <copyright file="PropertyProvider.cs" company="Abyssalsoft">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace Gui.ThemeEngine
{
    using System;
    using System.Collections.Generic;
    using System.Drawing;
    using System.Text;
    using System.Xml;

    /// <summary>
    /// Provides property value for the given attribute
    /// </summary>
    public static class PropertyProvider
    {
        /// <summary>
        /// Gets the value of the attribute, from the given node
        /// </summary>
        /// <param name="node">The xml source</param>
        /// <param name="attribute">The attribute to get</param>
        /// <param name="defaultValue">Default value to return in case the attribute is null</param>
        /// <returns>Gets the value of the attribute or defaultValue if is not specified in xml</returns>
        public static string GetValue(XmlNode node, string attribute, string defaultValue)
        {
            if (node.Attributes[attribute] != null)
            {
                return node.Attributes[attribute].Value;
            }
            else
            {
                return defaultValue;
            }
        }

        /// <summary>
        /// Gets the value of the attribute, from the given node
        /// </summary>
        /// <param name="node">The xml source</param>
        /// <param name="attribute">The attribute to get</param>
        /// <param name="defaultValue">Default value to return in case the attribute is null</param>
        /// <returns>Gets the value of the attribute or defaultValue if is not specified in xml</returns>
        public static int GetValue(XmlNode node, string attribute, int defaultValue)
        {
            if (node.Attributes[attribute] != null)
            {
                return Convert.ToInt32(node.Attributes[attribute].Value);
            }
            else
            {
                return defaultValue;
            }
        }

        /// <summary>
        /// Gets the value of the attribute, from the given node
        /// </summary>
        /// <param name="node">The xml source</param>
        /// <param name="attribute">The attribute to get</param>
        /// <param name="defaultValue">Default value to return in case the attribute is null</param>
        /// <returns>Gets the value of the attribute or defaultValue if is not specified in xml</returns>
        public static bool GetValue(XmlNode node, string attribute, bool defaultValue)
        {
            if (node.Attributes[attribute] != null)
            {
                if (node.Attributes[attribute].Value == "True")
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return defaultValue;
            }
        }

        /// <summary>
        /// Gets the value of the attribute, from the given node
        /// </summary>
        /// <param name="node">The xml source</param>
        /// <param name="attribute">The attribute to get</param>
        /// <returns>Gets the value of the attribute or defaultValue if is not specified in xml</returns>
        public static Color GetValue(XmlNode node, string attribute)
        {
            if (node.Attributes[attribute] != null)
            {
                string value = node.Attributes[attribute].Value.Trim();

                if (value == "Transparent")
                {
                    return Color.Transparent;
                }
                else
                {
                    string[] strArray = value.Split(',');
                    int r = Convert.ToInt32(strArray[0]);
                    int g = Convert.ToInt32(strArray[1]);
                    int b = Convert.ToInt32(strArray[2]);
                    return Color.FromArgb(r, g, b);
                }
            }
            else
            {
                return Color.Transparent;
            }
        }
    }
}
